//
//  WBStatusPictureView.swift
//  微博
//
//  Created by lipengfei on 15/9/9.
//  Copyright © 2015年 lipengfei. All rights reserved.
//

import UIKit
import SDWebImage
private let WBStatusPictureViewCellID = "WBStatusPictureCellID"
//每行最大图片数量
let WBStatusPictureMaxCount:CGFloat = 3
let WBStatusPictureItemMargin:CGFloat = 10
// 配图的最大尺寸
let WBStatusPictureMaxWidth = WBStatusPictureMaxCount * WBStatusPictureItemWidth + (WBStatusPictureMaxCount - 1) * WBStatusPictureItemMargin
//let WBStatusPictureItemWidth:CGFloat = 90

class WBStatusPictureView: UICollectionView {

    var statusViewModel:WBStatusViewModel?{
        didSet{
            sizeToFit()
            reloadData()
        }
    }
    override func sizeThatFits(size: CGSize) -> CGSize {
            return calcViewSize()
    }
    /// 根据模型中的图片数量计算视图大小
    private func calcViewSize() -> CGSize{
        let layout = collectionViewLayout as!UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: WBStatusPictureItemWidth, height: WBStatusPictureItemWidth)
        let count = statusViewModel?.thumbnailURLs?.count ?? 0
        
        if count == 0 {
            return CGSizeZero
        }
        if count == 1{
            let size = CGSize(width: 150, height: 150)
            layout.itemSize = size
            return size
        }
        if count == 4{
            let w = 2 * (WBStatusPictureItemWidth + WBStatusPictureItemMargin)
            return CGSize(width: w, height: w)
        }
        /*
            2,3
            5,6
            7,8,9
        */
        let row = CGFloat((count - 1) / (Int(WBStatusPictureMaxCount) + 1))
        let h = row * WBStatusPictureItemWidth + (row - 1) * WBStatusCellMargin
        let w = WBStatusPictureMaxWidth
        return CGSize(width: w, height: h)
        
    }
       //构造函数
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        backgroundColor = UIColor.lightGrayColor()
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        dataSource = self
        //注册重用cell
        registerClass(WBStatusPictureViewCell.self, forCellWithReuseIdentifier: WBStatusPictureViewCellID)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension WBStatusPictureView : UICollectionViewDataSource
{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       print(statusViewModel?.thumbnailURLs?.count)
        return statusViewModel?.thumbnailURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBStatusPictureViewCellID, forIndexPath: indexPath) as! WBStatusPictureViewCell
        cell.imageURL = statusViewModel?.thumbnailURLs![indexPath.item]
        return cell
    }
}
//配图cell
private class WBStatusPictureViewCell:UICollectionViewCell{
/// 初始化cell
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(iconView)
        iconView.ff_Fill(self)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
///  cell 中配图视图的url
    var imageURL:NSURL?{
        didSet{
            iconView.sd_setImageWithURL(imageURL)
        }
    }
    //懒加载 
    private let iconView:UIImageView = {
        let iv = UIImageView()
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
}
